package com.idreems.constellations;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebpageActivity extends Activity
{
	private String url;
	private WebView mWebView;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webpage);

		// TODO::url
		url = "http://m.sohu.com";

		mWebView = (WebView) findViewById(R.id.OriginalContent);
		mWebView.setFocusable(false);
		mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		final WebSettings settings = mWebView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		mWebView.setWebViewClient(new WebViewClient()
		{
			@Override
			public void onPageFinished(final WebView webView, final String url)
			{
				findViewById(R.id.OriginalProgress).setVisibility(View.GONE);
			}

			@Override
			public void onPageStarted(final WebView webView, final String url, final Bitmap favicon)
			{
				findViewById(R.id.OriginalProgress).setVisibility(View.VISIBLE);
			}

			@Override
			public boolean shouldOverrideUrlLoading(final WebView webView, final String url)
			{
				webView.loadUrl(url);
				return false;
			}
		});

		showOriginalPage();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
		{
			mWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		mWebView.stopLoading();
	}

	private void showOriginalPage()
	{
		mWebView.loadUrl(url);
	}
}
